package com.lc.question.controller;

import com.lc.question.pojo.PageBean;
import com.lc.question.pojo.TeaTab;
import com.lc.question.pojo.Teacher;
import com.lc.question.pojo.UserTab;
import com.lc.question.service.impl.TeacherServiceImpl;
import com.lc.question.service.impl.UserTabServiceImpl;
import com.lc.question.pojo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@Api(tags = "TeacherController|教职工接口类")
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    TeacherServiceImpl teacherService;
    @Autowired
    UserTabServiceImpl userTabService;

    @ApiOperation(value = "list(教职工信息)")
    @PostMapping("/list")
    @ResponseBody
    public List<Teacher> teacherList() {
        List<Teacher> teachers = teacherService.teacherList();
        return teachers;
    }

    @ApiOperation(value = "通过工号查询教职工")
    @PostMapping("/findById")
    @ResponseBody
    public Teacher findTeacherById(String tea_Id) {
        Teacher teacher = teacherService.teacherListById(tea_Id);
        return teacher;
    }

    @ApiOperation(value = "通过条件信息查询教职工集合")
    @PostMapping("/findById2")
    @ResponseBody
    public List<Teacher> findStudentById2(@RequestBody Teacher teacher) {
        List<Teacher> teachers = teacherService.teacherById2(teacher);
        return teachers;
    }

    @ApiOperation(value = "add(新增教职工)")
    @PostMapping("/add")
    @ResponseBody
    public void addTeacher(@RequestBody Teacher teacher) {
        teacherService.addTeacher(teacher);
    }

    //教师
    @ApiOperation(value = "list_tab(教职工信息展示表)")
    @PostMapping("/list_tab")
    @ResponseBody
    public PageBean<TeaTab> teacherList(@RequestBody TeaTab oldTeaTab, @RequestParam("currentPage") int currentPage, @RequestParam("total") int total){
        List<TeaTab> teaTabs = new ArrayList<>();

        UserTab userTab = oldTeaTab.getUserTab();
        Teacher tea = oldTeaTab.getTeacher();//给定要查询的student模板
        long totalNumber = userTabService.tabTotal(userTab);//总条数
        PageBean<TeaTab> planPageBean = new PageBean<TeaTab>(currentPage,total,totalNumber);
        int startIndex = planPageBean.getStartIndex();
        //传入tab_Id和par_state
        List<UserTab> tabs = userTabService.findTabChange_SurPar(userTab,startIndex,total);
        for (UserTab tab : tabs) {
            String user_id = tab.getUser_Id();
            tea.setTea_Id(user_id);
            //添加
            Teacher teacher = teacherService.teacherById(tea);
            //添加stuTab
            if (teacher!=null){
                TeaTab teaTab = new TeaTab(teacher,tab);
                teaTabs.add(teaTab);
            }
        }
        planPageBean.setPageDate(teaTabs);//设置返回数据集
        return planPageBean;
    }

}
